package org.mch.func.file.fileread;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JFileChooser;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;

/**
 * 描述：</BR>
 *
 * @author MC、ZL；
 *
 */
public class ClientEvent extends FuncEvent {

	private static final long serialVersionUID = 1L;

	JFileChooser fileChooser;

	public ClientEvent(FuncPanel panel) {
		super(panel);
	}

	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_FileName()) {
			readFileNames();
		}
	}

	public void readFileNames() {
		boolean isContainFolder = getPanel().getChckbx_folder().isSelected();// 是否包含【文件夹】
		boolean isContainFile = getPanel().getChckbx_file().isSelected();// 是否包含【文件】
		boolean isSearchChildFolder = getPanel().getChckbx_search_child().isSelected();// 是否搜索【子文件夹】
		boolean isOutFilePath = getPanel().getChckbx_outfilepath().isSelected();// 是否输出文件完整路径

		String prefix = getPanel().getText_prefix().getValue();// 前缀
		prefix = prefix == null ? "" : prefix;
		String suffix = getPanel().getText_suffix().getValue();// 后缀
		suffix = suffix == null ? "" : suffix;
		String liketext = getPanel().getText_like().getValue();// 模糊筛选

		File file = new File(getPanel().getText_path().getValue());
		StringBuffer sb = new StringBuffer();

		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			String outFileName = null;
			for (File tmpfile : files) {
				if (isOutFilePath) {
					outFileName = tmpfile.getPath();
				} else {
					outFileName = tmpfile.getName();
				}

				if (liketext != null && !tmpfile.getName().contains(liketext)) {
					continue;
				}

				if (tmpfile.isDirectory()) {
					if (isContainFolder) {
						sb.append(prefix);
						sb.append(outFileName);
						sb.append(suffix);
						sb.append("\n");
					}
					if (isSearchChildFolder) {
						recursionReadFile(tmpfile.listFiles(), sb, isContainFolder, isContainFile, isOutFilePath, prefix, suffix);
					}
				} else {
					if (isContainFile) {
						sb.append(prefix);
						if (suffix.equals("")) {
							sb.append(outFileName);
						} else {
							sb.append(tool.fileNameAddSuffix(outFileName, suffix));
						}
						sb.append("\n");
					}
				}
			}
			this.getPanel().getTextArea_up().setText(sb.toString());
		}
	}

	/**
	 * 递归读取子项
	 */
	private void recursionReadFile(File[] files, StringBuffer sb, boolean isContainFolder, boolean isContainFile,
			boolean isOutFilePath, String prefix, String suffix) {
		if (files != null && files.length > 0) {
			String outFileName = null;
			for (File tmpfile : files) {
				if (isOutFilePath) {
					outFileName = tmpfile.getPath();
				} else {
					outFileName = tmpfile.getName();
				}

				if (tmpfile.isDirectory()) {
					if (isContainFolder) {
						sb.append(prefix);
						sb.append(outFileName);
						sb.append(suffix);
						sb.append("\n");
					}
					recursionReadFile(tmpfile.listFiles(), sb, isContainFolder, isContainFile, isOutFilePath, prefix, suffix);
				} else {
					if (isContainFile) {
						sb.append(prefix);
						if (suffix.equals("")) {
							sb.append(outFileName);
						} else {
							sb.append(tool.fileNameAddSuffix(outFileName, suffix));
						}
						sb.append("\n");
					}
				}
			}
		}

	}

	public JFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// ����ѡ��Ŀ¼���ļ�
		}
		return fileChooser;
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}